package pt.ipleiria.estg.dei.ei.es2.bio;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import pt.ipleiria.estg.dei.ei.es2.bio.model.Contacto;


public class MainActivity extends Activity {

    private boolean hasCheckedIn = false;
    private Contacto myContact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myContact = new Contacto("Nuno Gomes", "2140624@ipleiria.pt");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_checkin) {
            provideConfirmationDialog(item);
        }

        return super.onOptionsItemSelected(item);
    }

    private void provideConfirmationDialog(final MenuItem item) {
        if(!hasCheckedIn)
            if(myContact != null){
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);

                alertBuilder.setTitle(getString(R.string.confirmationTitle));
                alertBuilder.setMessage(getString(R.string.confirmationMessage) + "\n" +
                    myContact.toString());

                alertBuilder.setIcon(android.R.drawable.ic_dialog_alert);

                alertBuilder.setPositiveButton(getString(R.string.oKBtn), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        checkIn(item);
                    }
                });

                alertBuilder.setNegativeButton(getString(R.string.cancelBtn), null);

                alertBuilder.show();
            }
    }

    private void checkIn(MenuItem item){
        if(hasConnectivity()) {
            hasCheckedIn = true;

            item.setIcon(R.drawable.ic_action_group);

            Intent intent = new Intent(getApplicationContext(), ParticipantsList.class);

            intent.putExtra(ParticipantsList.HASCHECKEDIN, hasCheckedIn);

            startActivity(intent);
        } else {
            Toast.makeText(getApplicationContext(), getString(R.string.noNetwork), Toast.LENGTH_LONG);
        }

    }

    private boolean hasConnectivity() {
        return true;
    }
}

